﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormTaiKhoanCapNhap : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public string id = "";
        public FormTaiKhoanCapNhap()
        {
            InitializeComponent();
        }

        private void BttLuu_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void BttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtTenTaiKhoan_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }


        private void txtMaTaiKhoan_TextChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void cbbTaiKhanCapTren_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }

        private void FormTaiKhoanCapNhap_Load(object sender, EventArgs e)
        {
            GetListTaiKhoan();
            if (id != "")
            {
                var t = db.TaiKhoans.Where(tt=>tt.Id==id).ToList();
                if(t.Count<1) return; 
                foreach (var taiKhoan in t)
                {
                    txtDienGiai.Text = taiKhoan.DienGiai;
                    txtMaTaiKhoan.Text = taiKhoan.Id;
                    cbbLoaiTaiKhoan.SelectedValue = taiKhoan.LoaiTaiKhoanId;
                    cbbTaiKhanCapTren.SelectedValue = taiKhoan.TaiKhoanCapTren;
                    txtTenTaiKhoan.Text = taiKhoan.TenTaiKhoan;
                }
                KiemTraDuLieu();
            }
            else
            {
                MessageBox.Show("Không tải được dữ liệu!");
                this.Close();
            }
        }

        void GetListTaiKhoan()
        {
            cbbTaiKhanCapTren.DataSource = db.TaiKhoans.ToList();
            cbbTaiKhanCapTren.DisplayMember = "Id";
            cbbTaiKhanCapTren.ValueMember = "Id";

            cbbLoaiTaiKhoan.DataSource = db.LoaiTaiKhoans.ToList();
            cbbTaiKhanCapTren.DisplayMember = "TenLoaiTaiKhoan";
            cbbTaiKhanCapTren.ValueMember = "Id";
        }
        private void Edit()
        {
            try
            {
                if (KiemTraMaTaiKhoan() == false)
                {
                    var t = db.TaiKhoans.Find(id);
                    t.TaiKhoanCapTren = cbbTaiKhanCapTren.SelectedValue.ToString();
                    t.LoaiTaiKhoanId = Convert.ToInt32(cbbLoaiTaiKhoan.SelectedValue);
                    t.TenTaiKhoan = txtTenTaiKhoan.Text;
                    t.DienGiai = txtDienGiai.Text;
                    db.Entry(t).State = EntityState.Modified;
                    db.SaveChanges();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Mã tài khoản: " + txtMaTaiKhoan.Text.Trim() + " đã tồn tại.");
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        private void KiemTraDuLieu()
        {
            if (txtMaTaiKhoan.Text != "" && Convert.ToInt32(cbbTaiKhanCapTren.SelectedValue) != 0 && Convert.ToInt32(cbbLoaiTaiKhoan.SelectedValue) != 0 && txtTenTaiKhoan.Text != "")
            {
                BttLuu.Enabled = true;
            }
            else
            {
                BttLuu.Enabled = false;
            }
        }
        private bool KiemTraMaTaiKhoan()
        {
            var t = db.TaiKhoans.Find(Convert.ToInt32(txtMaTaiKhoan.Text.Trim()));
            if (t != null)
            {
                return true;
            }
            return false;
        }

        private void cbbLoaiTaiKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            KiemTraDuLieu();
        }


    }
}
